home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 2 / ads / a-textio < prev    next >
Text File  |  1996-02-12  |  15KB  |  398 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                          A D A . T E X T _ I O                           --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.40 $                             --
  10. --                                                                          --
  11. -- This specification is adapted from the Ada Reference Manual for use with --
  12. -- GNAT.  In accordance with the copyright of that document, you can freely --
  13. -- copy and modify this specification,  provided that if you redistribute a --
  14. -- modified version,  any changes that you have made are clearly indicated. --
  15. --                                                                          --
  16. ------------------------------------------------------------------------------
  17.  
  18. --  Note: the generic subpackages of Text_IO (Integer_IO, Float_IO, Fixed_IO,
  19. --  Modular_IO, Decimal_IO and Enumeration_IO) appear as private children in
  20. --  GNAT. These children are with'ed automatically if they are referenced, so
  21. --  this rearrangement is invisible to user programs, but has the advantage
  22. --  that only the needed parts of Text_IO are processed and loaded.
  23.  
  24. with Ada.IO_Exceptions;
  25. with Ada.Streams;
  26. with System;
  27. with System.File_Control_Block;
  28. with System.Parameters;
  29.  
  30. package Ada.Text_IO is
  31.  
  32.    type File_Type is limited private;
  33.    type File_Mode is (In_File, Out_File, Append_File);
  34.  
  35.    --  The following representation clause allows the use of unchecked
  36.    --  conversion for rapid translation between the File_Mode type
  37.    --  used in this package and System.File_IO.
  38.  
  39.    for File_Mode use
  40.      (In_File     => 0,  -- System.FIle_IO.File_Mode'Pos (In_File)
  41.       Out_File    => 2,  -- System.File_IO.File_Mode'Pos (Out_File)
  42.       Append_File => 3); -- System.File_IO.File_Mode'Pos (Append_File)
  43.  
  44.    type Count is range 0 .. System.Parameters.Count_Max;
  45.  
  46.    subtype Positive_Count is Count range 1 .. Count'Last;
  47.  
  48.    Unbounded : constant Count := 0;
  49.    --  Line and page length
  50.  
  51.    subtype Field is Integer range 0 .. System.Parameters.Field_Max;
  52.  
  53.    subtype Number_Base is Integer range 2 .. 16;
  54.  
  55.    type Type_Set is (Lower_Case, Upper_Case);
  56.  
  57.    ---------------------
  58.    -- File Management --
  59.    ---------------------
  60.  
  61.    procedure Create
  62.      (File : in out File_Type;
  63.       Mode : in File_Mode := Out_File;
  64.       Name : in String := "";
  65.       Form : in String := "");
  66.  
  67.    procedure Open
  68.      (File : in out File_Type;
  69.       Mode : in File_Mode;
  70.       Name : in String;
  71.       Form : in String := "");
  72.  
  73.    procedure Close  (File : in out File_Type);
  74.    procedure Delete (File : in out File_Type);
  75.    procedure Reset  (File : in out File_Type; Mode : in File_Mode);
  76.    procedure Reset  (File : in out File_Type);
  77.  
  78.    function Mode (File : in File_Type) return File_Mode;
  79.    function Name (File : in File_Type) return String;
  80.    function Form (File : in File_Type) return String;
  81.  
  82.    function Is_Open (File : in File_Type) return Boolean;
  83.  
  84.    ------------------------------------------------------
  85.    -- Control of default input, output and error files --
  86.    ------------------------------------------------------
  87.  
  88.    procedure Set_Input  (File : in File_Type);
  89.    procedure Set_Output (File : in File_Type);
  90.    procedure Set_Error  (File : in File_Type);
  91.  
  92.    function Standard_Input  return File_Type;
  93.    function Standard_Output return File_Type;
  94.    function Standard_Error  return File_Type;
  95.  
  96.    function Current_Input  return File_Type;
  97.    function Current_Output return File_Type;
  98.    function Current_Error  return File_Type;
  99.  
  100.    type File_Access is access constant File_Type;
  101.  
  102.    function Standard_Input  return File_Access;
  103.    function Standard_Output return File_Access;
  104.    function Standard_Error  return File_Access;
  105.  
  106.    function Current_Input  return File_Access;
  107.    function Current_Output return File_Access;
  108.    function Current_Error  return File_Access;
  109.  
  110.    --------------------
  111.    -- Buffer control --
  112.    --------------------
  113.  
  114.    --  Note: The paramter file is in out in the RM, but as pointed out
  115.    --  in <<95-5166.a Tucker Taft 95-6-23>> this is clearly an oversight.
  116.  
  117.    procedure Flush (File : in File_Type);
  118.    procedure Flush;
  119.  
  120.    --------------------------------------------
  121.    -- Specification of line and page lengths --
  122.    --------------------------------------------
  123.  
  124.    procedure Set_Line_Length (File : in File_Type; To : in Count);
  125.    procedure Set_Line_Length (To : in Count);
  126.  
  127.    procedure Set_Page_Length (File : in File_Type; To : in Count);
  128.    procedure Set_Page_Length (To : in Count);
  129.  
  130.    function Line_Length (File : in File_Type) return Count;
  131.    function Line_Length return Count;
  132.  
  133.    function Page_Length (File : in File_Type) return Count;
  134.    function Page_Length return Count;
  135.  
  136.    ------------------------------------
  137.    -- Column, Line, and Page Control --
  138.    ------------------------------------
  139.  
  140.    procedure New_Line (File : in File_Type; Spacing : in Positive_Count := 1);
  141.    procedure New_Line (Spacing : in Positive_Count := 1);
  142.  
  143.    procedure Skip_Line (File : in File_Type; Spacing : in Positive_Count := 1);
  144.    procedure Skip_Line (Spacing : in Positive_Count := 1);
  145.  
  146.    function End_Of_Line (File : in File_Type) return Boolean;
  147.    function End_Of_Line return Boolean;
  148.  
  149.    procedure New_Page (File : in File_Type);
  150.    procedure New_Page;
  151.  
  152.    procedure Skip_Page (File : in File_Type);
  153.    procedure Skip_Page;
  154.  
  155.    function End_Of_Page (File : in File_Type) return Boolean;
  156.    function End_Of_Page return Boolean;
  157.  
  158.    function End_Of_File (File : in File_Type) return Boolean;
  159.    function End_Of_File return Boolean;
  160.  
  161.    procedure Set_Col (File : in File_Type;  To : in Positive_Count);
  162.    procedure Set_Col (To : in Positive_Count);
  163.  
  164.    procedure Set_Line (File : in File_Type; To : in Positive_Count);
  165.    procedure Set_Line (To : in Positive_Count);
  166.  
  167.    function Col (File : in File_Type) return Positive_Count;
  168.    function Col return Positive_Count;
  169.  
  170.    function Line (File : in File_Type) return Positive_Count;
  171.    function Line return Positive_Count;
  172.  
  173.    function Page (File : in File_Type) return Positive_Count;
  174.    function Page return Positive_Count;
  175.  
  176.    -----------------------------
  177.    -- Characters Input-Output --
  178.    -----------------------------
  179.  
  180.    procedure Get (File : in File_Type; Item : out Character);
  181.    procedure Get (Item : out Character);
  182.    procedure Put (File : in File_Type; Item : in Character);
  183.    procedure Put (Item : in Character);
  184.  
  185.    procedure Look_Ahead
  186.      (File        : in File_Type;
  187.       Item        : out Character;
  188.       End_Of_Line : out Boolean);
  189.  
  190.    procedure Look_Ahead
  191.      (Item        : out Character;
  192.       End_Of_Line : out Boolean);
  193.  
  194.    procedure Get_Immediate
  195.      (File : in File_Type;
  196.       Item : out Character);
  197.  
  198.    procedure Get_Immediate
  199.      (Item : out Character);
  200.  
  201.    procedure Get_Immediate
  202.      (File      : in File_Type;
  203.       Item      : out Character;
  204.       Available : out Boolean);
  205.  
  206.    procedure Get_Immediate
  207.      (Item      : out Character;
  208.       Available : out Boolean);
  209.  
  210.    --------------------------
  211.    -- Strings Input-Output --
  212.    --------------------------
  213.  
  214.    procedure Get (File : in File_Type; Item : out String);
  215.    procedure Get (Item : out String);
  216.    procedure Put (File : in File_Type; Item : in String);
  217.    procedure Put (Item : in String);
  218.  
  219.    procedure Get_Line
  220.      (File : in File_Type;
  221.       Item : out String;
  222.       Last : out Natural);
  223.  
  224.    procedure Get_Line
  225.      (Item : out String;
  226.       Last : out Natural);
  227.  
  228.    procedure Put_Line
  229.      (File : in File_Type;
  230.       Item : in String);
  231.  
  232.    procedure Put_Line
  233.      (Item : in String);
  234.  
  235.    --  Exceptions
  236.  
  237.    Status_Error : exception renames IO_Exceptions.Status_Error;
  238.    Mode_Error   : exception renames IO_Exceptions.Mode_Error;
  239.    Name_Error   : exception renames IO_Exceptions.Name_Error;
  240.    Use_Error    : exception renames IO_Exceptions.Use_Error;
  241.    Device_Error : exception renames IO_Exceptions.Device_Error;
  242.    End_Error    : exception renames IO_Exceptions.End_Error;
  243.    Data_Error   : exception renames IO_Exceptions.Data_Error;
  244.    Layout_Error : exception renames IO_Exceptions.Layout_Error;
  245.  
  246. private
  247.    -----------------------------------
  248.    -- Handling of Format Characters --
  249.    -----------------------------------
  250.  
  251.    --  Line marks are represented by the single character Ascii.LF (16#0A#).
  252.    --  In DOS and similar systems, underlying file translation takes care
  253.    --  of translating this to and from the standard CR/LF sequences used in
  254.    --  these operating systems to mark the end of a line. On output there is
  255.    --  always a line mark at the end of the last line, but on input, this
  256.    --  line mark can be omitted, and is implied by the end of file.
  257.  
  258.    --  Page marks are represented by the single character Ascii.FF (16#0C#),
  259.    --  The page mark at the end of the file may be omitted, and is normally
  260.    --  omitted on output unless an explicit New_Page call is made before
  261.    --  closing the file. No page mark is added when a file is appended to,
  262.    --  so, in accordance with the permission in (RM A.10.2(4)), there may
  263.    --  or may not be a page mark separating preexising text in the file
  264.    --  from the new text to be written.
  265.  
  266.    --  A file mark is marked by the physical end of file. In DOS translation
  267.    --  mode on input, an EOF character (SUB = 16#1A#) gets translated to the
  268.    --  physical end of file, so in effect this character is recognized as
  269.    --  marking the end of file in DOS and similar systems.
  270.  
  271.    LM : constant := Character'Pos (Ascii.LF);
  272.    --  Used as line mark
  273.  
  274.    PM : constant := Character'Pos (Ascii.FF);
  275.    --  Used as page mark, except at end of file where it is implied
  276.  
  277.    --------------------------------
  278.    -- Text_IO File Control Block --
  279.    --------------------------------
  280.  
  281.    package FCB renames System.File_Control_Block;
  282.  
  283.    type Text_AFCB is new FCB.AFCB with record
  284.       Page        : Count := 1;
  285.       Line        : Count := 1;
  286.       Col         : Count := 1;
  287.       Line_Length : Count := 0;
  288.       Page_Length : Count := 0;
  289.  
  290.       Before_LM : Boolean := False;
  291.       --  This flag is used to deal with the anomolies introduced by the
  292.       --  peculiar definition of End_Of_File and End_Of_Page in Ada. These
  293.       --  functions require looking ahead more than one character. Since
  294.       --  there is no convenient way of backing up more than one character,
  295.       --  what we do is to leave ourselves positioned past the LM, but set
  296.       --  this flag, so that we know that from an Ada point of view we are
  297.       --  in front of the LM, not after it. A bit of a kludge, but it works!
  298.  
  299.       Before_LM_PM : Boolean := False;
  300.       --  This flag similarly handles the case of being physically positioned
  301.       --  after a LM-PM sequence when logically we are before the LM-PM. This
  302.       --  flag can only be set if Before_LM is also set.
  303.  
  304.       Before_Wide_Character : Boolean := False;
  305.       --  This flag is always False for Text_IO files. It is used in the wide
  306.       --  character case to indicate that a wide character in the input has
  307.       --  been read by Wide_Text_IO.Look_Ahead. If it is set to True, then it
  308.       --  means that the stream is logically positioned before the character
  309.       --  but is physically positioned after it. The character involved must
  310.       --  not be in the range 16#00#-16#7F#, i.e. if the flag is set, then we
  311.       --  know the next character has a code greater than 16#7F#. The reason
  312.       --  we define this flag in the base Text_IO control block is that there
  313.       --  are routines shared between Text_IO and Wide_Text_IO, notably the
  314.       --  numeric input routines in Ada.Text_IO.Generic_Aux, which need to
  315.       --  test the flag (note that if it is set, the next character cannot
  316.       --  be a valid character in a numeric item).
  317.  
  318.    end record;
  319.  
  320.    type File_Type is access all Text_AFCB;
  321.  
  322.    function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr;
  323.  
  324.    procedure AFCB_Close (File : access Text_AFCB);
  325.    procedure AFCB_Free  (File : access Text_AFCB);
  326.  
  327.    procedure Read
  328.      (File : in out Text_AFCB;
  329.       Item : out Ada.Streams.Stream_Element_Array;
  330.       Last : out Ada.Streams.Stream_Element_Offset);
  331.    --  Read operation used when Text_IO file is treated directly as Stream
  332.  
  333.    procedure Write
  334.      (File : in out Text_AFCB;
  335.       Item : in Ada.Streams.Stream_Element_Array);
  336.    --  Write operation used when Text_IO file is treated directly as Stream
  337.  
  338.    ------------------------
  339.    -- The Standard Files --
  340.    ------------------------
  341.  
  342.    Null_Str : aliased constant String := "";
  343.    --  Used as name and form of standard files
  344.  
  345.    Standard_Err_AFCB : aliased Text_AFCB;
  346.    Standard_In_AFCB  : aliased Text_AFCB;
  347.    Standard_Out_AFCB : aliased Text_AFCB;
  348.  
  349.    Standard_Err : aliased File_Type := Standard_Err_AFCB'Access;
  350.    Standard_In  : aliased File_Type := Standard_In_AFCB'Access;
  351.    Standard_Out : aliased File_Type := Standard_Out_AFCB'Access;
  352.    --  Standard files
  353.  
  354.    Current_In   : aliased File_Type := Standard_In;
  355.    Current_Out  : aliased File_Type := Standard_Out;
  356.    Current_Err  : aliased File_Type := Standard_Err;
  357.    --  Current files
  358.  
  359.    -----------------------
  360.    -- Local Subprograms --
  361.    -----------------------
  362.  
  363.    --  These subprograms are in the private part of the spec so that they can
  364.    --  be shared by the routines in the body of Ada.Text_IO.Wide_Text_IO.
  365.  
  366.    --  Note: we use Integer in these declarations instead of the more accurate
  367.    --  Interfaces.C_Streams.int, because we do not want to drag in the spec of
  368.    --  this interfaces package with the spec of Ada.Text_IO, and we know that
  369.    --  in fact these types are identical
  370.  
  371.    function Getc (File : File_Type) return Integer;
  372.    --  Gets next character from file, which has already been checked for
  373.    --  being in read status, and returns the character read if no error
  374.    --  occurs. The result is EOF if the end of file was read.
  375.  
  376.    function Nextc (File : File_Type) return Integer;
  377.    --  Returns next character from file without skipping past it (i.e. it
  378.    --  is a combination of Getc followed by an Ungetc).
  379.  
  380.    procedure Putc (ch : Integer; File : File_Type);
  381.    --  Outputs the given character to the file, which has already been
  382.    --  checked for being in output status. Device_Error is raised if the
  383.    --  character cannot be written.
  384.  
  385.    procedure Terminate_Line (File : File_Type);
  386.    --  If the file is in Write_File or Append_File mode, and the current
  387.    --  line is not terminated, then a line terminator is written using
  388.    --  New_Line. Note that there is no Terminate_Page routine, because
  389.    --  the page mark at the end of the file is implied if necessary.
  390.  
  391.    procedure Ungetc (ch : Integer; File : File_Type);
  392.    --  Pushes back character into stream, using ungetc. The caller has
  393.    --  checked that the file is in read status. Device_Error is raised
  394.    --  if the character cannot be pushed back. An attempt to push back
  395.    --  and end of file character (EOF) is ignored.
  396.  
  397. end Ada.Text_IO;
  398.